﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Animation;
using MapLibrary.Particles.Emitters;
using MapLibrary.Players.CollisionResponse;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using UtilityLibrary;

namespace Enemies
{
    class Enemy : Sprite, IDamageable
    {
        public static Texture2D healthBarTexture;
        protected const int HEALTH_BAR_HEIGHT = 5;
        protected const int HEALTH_BAR_WIDTH = 40;

        protected int health;
        protected int maxHealth;

        protected int[] proteinAmounts;
        protected ProteinEmitter proteinEmitter;

        public Enemy() : base()
        {
            //all enemies drop the same amount for now
            proteinAmounts = new int[] { 50, 100, 20 };

            OnInitialization += SetEmitters;

            Hookable = false;
        }

        protected void SetEmitters()
        {
            proteinEmitter = new ProteinEmitter(World.ParticleManager, this, proteinAmounts);
        }

        protected virtual void Die()
        {
            proteinEmitter.Trigger();
        }

        public virtual void DealDamage(int amount)
        {
            health -= amount;
            if (health <= 0)
            {
                Die();
                World.CurrentLevel.AddBodyToRemove(this);
            }
        }

        public override void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch batch, MapLibrary.WorldViewport viewport)
        {
            base.Draw(batch, viewport);
            if (health > 0 && health < maxHealth)
            {
                if (healthBarTexture == null)
                {
                    healthBarTexture = World.ContentManager.Load<Texture2D>(@"whitebox");
                }

                float percent = ((float)health) / ((float)maxHealth);
                float width = HEALTH_BAR_WIDTH * percent;

                Vector2 position = this.GetPosition();
                position.Y -= (Type.Height / 2.0f) + 5;
                position = viewport.WorldToViewportCoords(position);
                //PrimitiveBrush.RenderPoint(batch, position);

                position.X -= HEALTH_BAR_WIDTH / 2.0f;
                Rectangle rect = new Rectangle((int)position.X, (int)position.Y, HEALTH_BAR_WIDTH, HEALTH_BAR_HEIGHT);
                batch.Draw(healthBarTexture, rect, Color.Black);
                rect.Width = (int)width;

                Color color;
                if (percent >= .75f)
                {
                    color = Color.Green;
                }
                else if (percent > .25)
                {
                    color = Color.Yellow;
                }
                else
                {
                    color = Color.Red;
                }
                batch.Draw(healthBarTexture, rect, color);

            }
        }
    }
}
